package com.wolfalpha.lwtool.activity;

import com.wolfalpha.lwtool.R;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;

/**
 * 设置中心
 * 
 * @author ttStudio
 * 
 */
public class SettingActivity extends PreferenceActivity implements
		OnPreferenceChangeListener, OnPreferenceClickListener {

	private CheckBoxPreference onCheckNotify;
	private CheckBoxPreference onCheckStartAuto;
	private Preference setSpeed;
	private Preference aboutApp;
	private SharedPreferences shp;
	private Editor editor;

	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting);
		addPreferencesFromResource(R.xml.setting);
		// 获取存储
		shp = PreferenceManager.getDefaultSharedPreferences(this);
		editor = shp.edit();

		// 绑定Preference
		onCheckNotify = (CheckBoxPreference) findPreference("check_notify");
		onCheckStartAuto = (CheckBoxPreference) findPreference("start_auto");
		setSpeed = (Preference) findPreference("set_speed");
		aboutApp = (Preference) findPreference("aboutApp");
		// 注册监听
		onCheckNotify.setOnPreferenceChangeListener(this);
		onCheckStartAuto.setOnPreferenceChangeListener(this);
		setSpeed.setOnPreferenceClickListener(this);
		aboutApp.setOnPreferenceClickListener(this);

		setSpeed.setSummary("当前频率：" + this.getResources().getStringArray(
				R.array.frequency)[shp.getInt("frequency", 0)]);
	}

	@Override
	public boolean onPreferenceClick(Preference preference) {
		Log.v("SystemSetting", "preference is changed");
		Log.v("Key_SystemSetting", preference.getKey());
		// 判断是哪个Preference改变了
		if (preference == aboutApp) {
			AlertDialog.Builder builder = new AlertDialog.Builder(
					SettingActivity.this);
			builder.setTitle("关于");
			builder.setMessage("powered by 狼王网络科技有限公司\n\nCopyright-2015-\n官网lwapp.sinaapp.com");
			builder.setPositiveButton("去访问App官网", new OnClickListener() {
				@Override
				public void onClick(DialogInterface arg0, int arg1) {
					Uri uri = Uri.parse("http://lwapp.sinaapp.com");
					Intent intent = new Intent();
					intent.setAction("android.intent.action.VIEW");
					intent.setData(uri);
					SettingActivity.this.startActivity(intent);
				}
			});
			builder.setNegativeButton("知道了", null);
			builder.show();
		} else if (preference == setSpeed) {
			AlertDialog.Builder builder = new AlertDialog.Builder(
					SettingActivity.this);
			builder.setTitle("选择频率");
			// 设置一个下拉的列表选择项
			builder.setItems(getResources().getStringArray(R.array.frequency),
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							Toast.makeText(
									SettingActivity.this,
									"选择的频率为："
											+ getResources().getStringArray(
													R.array.frequency)[which],
									Toast.LENGTH_SHORT).show();

							editor.putInt("frequency", which);
							editor.commit();

							setSpeed.setSummary("当前频率："
									+ getResources().getStringArray(
											R.array.frequency)[which]);
						}
					});
			builder.show();
		} else {
			// 如果返回false表示不允许被改变
			return false;
		}
		// 返回true表示允许改变
		return true;
	}

	@Override
	public boolean onPreferenceChange(Preference preference, Object arg1) {
		// 判断是哪个Preference被点击了
		if (preference == onCheckNotify) {
			Toast.makeText(this, "该功能正在开发", Toast.LENGTH_SHORT).show();
		} else if (preference == onCheckStartAuto) {
			Toast.makeText(this, "该功能正在开发", Toast.LENGTH_SHORT).show();
		} else if (preference == setSpeed) {
			// Toast.makeText(this, "3", Toast.LENGTH_SHORT).show();
		} else {
			return false;
		}
		return true;
	}

}